package com.fl.dao;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
public class AlipayIumpSum {

    //实例化客户端,填入所需参数
    private static AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
            "2021000118606047",
            "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCxJ3d8MYUrrJ+Bte5q8v9JOxw3YGbrkxNNufiBwb0cKjxRDg5QADl149+nn/REgWmR6dLCatVZYy2avFcuUuYd0/xdPYZskUb6YtGrghwPVzmuNAPORBnt6hBGWJRduNIBnyoONRzXugJRRFDa3Mr3l9Ca0J4+AzNdXzEYXWU5Xci3c7uruZ9XRoLBrjvmMPL71e9hnJQ5Z9qGSq92qdgh83jUE189HcmmFVqwpAaMQhEkTukuKwjy6UF6FzoYO7dH/wzQJBeFJv1ePm90dJmQLh7gvZ8TdyVrDlx0RT9vP2l1EkCTww7jaUteOPXzCdgi9PqOTbgtQNLdVeSB0R9HAgMBAAECggEBAKVc+MzGENAGz9GteIL+lsvN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F0scqpQSPMbUzWEoyjV7vU7swv40oBC9oyyXOO8P6j72wmrraw8/SAtX1YZBtG6tJeMIUIenGqA68FvEiul7fCwOPO85GKtSpypoidjnxFX+Fqj0g==",
            "json",
            "utf-8",
            "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlXQ2puhU475iJGI1WEM2ArLu1i5uhdrLa8WpHUB4pc9ZrGWAdJZd3UVr9NuBb6+4x4LZILTWHrIr/7AjaIaOb7ub4JnFNnpy61U7bXHUANV8kkjwMxPYLXdg05Yoo5MsnGyUYUmlb8Zyik6t5BktyUpcEoGAmxpAPZoLMCkKme5jbFLzv1VzWxCfGXQKsJoOHfTMG2xQXEk7MR3+8/MLVpuffqgNfdrvxpzH1vlvbVs0L3//t/eOH+lOUHhF/fM6Lcc68KBNN330TIXS8+wl+FDfQkcLL1kL7YfK6W5fSFaMzJkhuq8voyz+bW8ngaTYuh3MkK7x04+GqUsaj8TQKQIDAQAB",
            "RSA2");

    /**
     * 快捷支付调用支付宝支付接口
     *
     * @param model，id，payables，
     * @return Object
     */
    @RequestMapping("alipaySum")
    public String alipayIumpSum(Model model, String WIDout_trade_no, String WIDsubject, String WIDtotal_amount, String WIDbody,
                                HttpServletResponse response)
            throws Exception {
        System.out.println("请求进来" + WIDtotal_amount);
        String payables = WIDtotal_amount;
        // 订单名称，必填(必须是数字)
        String subject = WIDsubject;
        // 付款金额，必填
        String total_fee = WIDout_trade_no;
        // 商品描述，可空
        String body = WIDbody;
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", "2021000118606047", "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCxJ3d8MYUrrJ+Bte5q8v9JOxw3YGbrkxNNufiBwb0cKjxRDg5QADl149+nn/REgWmR6dLCatVZYy2avFcuUuYd0/xdPYZskUb6YtGrghwPVzmuNAPORBnt6hBGWJRduNIBnyoONRzXugJRRFDa3Mr3l9Ca0J4+AzN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rDFyt4sOU/ZNdqRlGpXatxqhS8iSrKMlYV3kkJLst3JvVkd72KC8k6nV2fNa1KZn2RRH3nhIbq+lNeIg6vlJZT5DrSD18wU3UxV8/lkD3OjFZpRRpfMJV4PnsHqaURyxqEy22KV4mOFnI1KO6aSFO3XwKBgQC7GdDHszlMyD1rSUP1RiMXoUZq/S+IY20gZbdAXdlLYtUInRLSEpyTJhIhHF0scqpQSPMbUzWEoyjV7vU7swv40oBC9oyyXOO8P6j72wmrraw8/SAtX1YZBtG6tJeMIUIenGqA68FvEiul7fCwOPO85GKtSpypoidjnxFX+Fqj0g==", "json", "utf-8", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlXQ2puhU475iJGI1WEM2ArLu1i5uhdrLa8WpHUB4pc9ZrGWAdJZd3UVr9NuBb6+4x4LZILTWHrIr/7AjaIaOb7ub4JnFNnpy61U7bXHUANV8kkjwMxPYLXdg05Yoo5MsnGyUYUmlb8Zyik6t5BktyUpcEoGAmxpAPZoLMCkKme5jbFLzv1VzWxCfGXQKsJoOHfTMG2xQXEk7MR3+8/MLVpuffqgNfdrvxpzH1vlvbVs0L3//t/eOH+lOUHhF/fM6Lcc68KBNN330TIXS8+wl+FDfQkcLL1kL7YfK6W5fSFaMzJkhuq8voyz+bW8ngaTYuh3MkK7x04+GqUsaj8TQKQIDAQAB", "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model1 = new AlipayTradePrecreateModel();
        request.setBizModel(model1);
        model1.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
        model1.setTotalAmount(total_fee);
        model1.setSubject(subject);
        AlipayTradePrecreateResponse response1 = alipayClient.execute(request);
        System.out.print(response1.getBody());
        System.out.print(response1.getQrCode());
        System.out.println("订单名称：" + subject + ",付款金额：" + total_fee + "，商品描述：" + body);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = sdf.format(new Date());
        // 付款金额，必填
        String total_amount = payables.replace(",", "");

//        response.setContentType("text/html; charset=gbk");
//        PrintWriter out = response.getWriter();
//        out.print(result);
        return "success";
    }

    @GetMapping("/createGrade")
    public void createGrade(HttpServletResponse response)  {
      try {
          AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        String outTradeNo = "423123213211232324"; // 商户订单号，商户网站订单系统中唯一订单号，必填
        String totalAmount = "99.9"; // 付款金额，必填
        String subject = "羽绒服"; // 订单名称，必填
        String body = "冬日神器"; // 商品描述，可空
        request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        // 在公共参数中设置回跳和通知地址
        request.setReturnUrl("http://5t4x9a.natappfree.cc/success");
        request.setNotifyUrl("http://5t4x9a.natappfree.cc/nodify");
          String form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成html表单

    } catch (AlipayApiException  e) {
        e.printStackTrace();
    }
    }

    @GetMapping("/pay")
    public void appy(
//            @RequestParam("prodcuer_name") String producerName,
//                     @RequestParam("totalPrice") String totalPrice,
//                     @RequestParam("comment") String comment ,
            HttpServletResponse response) {
        try {

            AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();
            String outTradeNo = "423123213211232324"; // 商户订单号，商户网站订单系统中唯一订单号，必填
            String totalAmount = "99.9"; // 付款金额，必填
            String subject = "羽绒服"; // 订单名称，必填
            String body = "冬日神器"; // 商品描述，可空
            payRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                    + "\"total_amount\":\"" + totalAmount + "\","
                    + "\"subject\":\"" + subject + "\","
                    + "\"body\":\"" + body + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            // 在公共参数中设置回跳和通知地址
            payRequest.setReturnUrl("http://5t4x9a.natappfree.cc/success");
            payRequest.setNotifyUrl("http://5t4x9a.natappfree.cc/nodify");
            String form = alipayClient.pageExecute(payRequest).getBody(); // 调用SDK生成html表单
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(form);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException | IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/lookMoney")
    public void getMoney(HttpServletResponse response) throws AlipayApiException, IOException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000118606047",
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCxJ3d8MYUrrJ+Bte5q8v9JOxw3YGbrkxNNufiBwb0cKjxRDg5QADl149+nn/REgWmR6dLCatVZYy2avFcuUuYd0/xdPYZskUb6YtGrghwPVzmuNAPORBnt6hBGWJRduNIBnyoONRzXugJRRFDa3Mr3l9Ca0J4+AzNdXzEYXWU5Xci3c7uruZ9XRoLBrjvmMPL71e9hnJQ5Z9qGSq92qdgh83jUE189HcmmFVqwpAaMQhEkTukuKwjy6UF6FzoYO7dH/wzQJBeFJv1ePm90dJmQLh7gvZ8TdyVrDlx0RT9vP2l1EkCTww7jaUteOPXzCdgi9PqOTbgtQNLdVeSB0R9HAgMBAAECggEBAKVc+MzGENAGz9GteIL+lsvN0HXkl824OSGpwc2lghgjWLA0lamZvAlOj3wR9FpqDph0khaG0OYijPROcHa+TS10rrJ+dtcq+P1KdvtIdVbgjegNh6vnp+iMP2razot5tRZscz5V17mLWRRw16SkyxU48jFBISUzqaOz8skSjSVw1wg1Cr0ScHU8tP9BJ+ELXs/3JDM/w8erCjaVVbpmCDmegq6Zu3WmqiLj+EEmxa3oe0pAilgKwAKs1ot0UlYLuYvuTXf3S5R42AVnmkS/bOFwgNiN+5AvwZYZZ/s/1ld9W6HvjEGQ6A2vdPwWiG4XE4jC7clFCbT7YYIyiQwWHgECgYEA2BYJ6kJyz1OnR7BeguSWl5I6gZ+H51hJsgMf28/pDYQSLly+kmdKklM8yznXI21emhDeN3xM9KGmKOqpI0CsFtbKhiNp2DKO/Fg/t2nooPNVkUget2hpxuZKZZWAVmCqRY0tZrVblQxJUd/2QCxeDJLJV84ztRdd5QCBfGIGQwECgYEA0eB494eCrDYI2hugSDb7bjeczxsEyBlgvNH+FArvqoJEJjwByaUX8U146fdmlpzirohstkMW3I75iCpZSbKFmQDTRHV2AlPCcGtle/5FJGbw4+W3ahJTU3i4zrtZup0WAhEmeqlszf8mSmtNOV20J1OIL9MgRcGdQGdaHI/2ikcCgYEAy423OV3vGS8MXZPe9B6xXYb6ZjEJrPyWL4KR+Fcaiwjmt5XWT8PNk5mloyuRZmYYv7k0G8CUHb5I8DJdsj+x3LHphgLGKks2NrBLFVRHN5cB/CGcDdnmCt/VJWx3wOa+f1ZGdoJRC07Porj/F+Szk9SSpowwWV7xrE+Vqr4MeAECgYASDVG6UMCPmXI3eGrX60rDFyt4sOU/ZNdqRlGpXatxqhS8iSrKMlYV3kkJLst3JvVkd72KC8k6nV2fNa1KZn2RRH3nhIbq+lNeIg6vlJZT5DrSD18wU3UxV8/lkD3OjFZpRRpfMJV4PnsHqaURyxqEy22KV4mOFnI1KO6aSFO3XwKBgQC7GdDHszlMyD1rSUP1RiMXoUZq/S+IY20gZbdAXdlLYtUInRLSEpyTJhIhHF0scqpQSPMbUzWEoyjV7vU7swv40oBC9oyyXOO8P6j72wmrraw8/SAtX1YZBtG6tJeMIUIenGqA68FvEiul7fCwOPO85GKtSpypoidjnxFX+Fqj0g==",
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlXQ2puhU475iJGI1WEM2ArLu1i5uhdrLa8WpHUB4pc9ZrGWAdJZd3UVr9NuBb6+4x4LZILTWHrIr/7AjaIaOb7ub4JnFNnpy61U7bXHUANV8kkjwMxPYLXdg05Yoo5MsnGyUYUmlb8Zyik6t5BktyUpcEoGAmxpAPZoLMCkKme5jbFLzv1VzWxCfGXQKsJoOHfTMG2xQXEk7MR3+8/MLVpuffqgNfdrvxpzH1vlvbVs0L3//t/eOH+lOUHhF/fM6Lcc68KBNN330TIXS8+wl+FDfQkcLL1kL7YfK6W5fSFaMzJkhuq8voyz+bW8ngaTYuh3MkK7x04+GqUsaj8TQKQIDAQAB",
                "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\"HZ0120131127001\"," +
                "      \"query_options\":[" +
                "        \"trade_settle_info\"" +
                "      ]" +
                "  }");
        AlipayTradeQueryResponse response1 = alipayClient.execute(request);
        String body = response1.getBody();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(body);
        response.getWriter().flush();
    }

    @GetMapping("/resetMoney")
    public void restMoney(HttpServletResponse response) throws Exception{
        AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\"HZ0120131127001\"," +
                "\"trade_no\":\"2013112611001004680073956707\"" +
                "  }");
        AlipayTradeCancelResponse alipayTradeCancelResponse = alipayClient.execute(request);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(alipayTradeCancelResponse.getBody());
        response.getWriter().flush();
    }



    @RequestMapping("/app")
    public String getApp() {
        return "page";
    }

    @RequestMapping("/success")
    public String success() {
        return "success";
    }

    @RequestMapping("/nodify")
    public String getNo(HttpServletRequest request, HttpServletResponse response) {
        String orderId = request.getParameter("out_trade_no");
        System.out.println(orderId); // 输出订单号
        return "shop";
    }
}